import com.gin.pixiv.callback.PixivCallback;
import com.gin.pixiv.enums.Language;
import com.gin.pixiv.main.Pixiv;
import com.gin.pixiv.response.illustration.IllustrationDetail;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.stream.Collectors;

/**
 * @author bx002
 * @since 2024/2/19 14:07
 */
public class JavaDemo {
    public static void main(String[] args) {
        final String cookie = readText(new File("D:/Working/pixiv-cookie.txt"));
        assert cookie != null;

        // 如果没有预先保存token ，先获取token
        OkHttpClient okHttpClient = Pixiv.obtainOkHttpClient(cookie, null, Language.zh);
        Retrofit retrofit = Pixiv.obtainRetrofit(okHttpClient, Pixiv.DEFAULT_HOST);
        Pixiv pixiv = new Pixiv(retrofit);

        final String token = pixiv.fetchToken();

//        final String token = readText(new File("D:/Working/pixiv-token.txt"));
        // 如果已经保存了token，直接从这里开始
        okHttpClient = Pixiv.obtainOkHttpClient(cookie, token, Language.zh);
        retrofit = Pixiv.obtainRetrofit(okHttpClient, Pixiv.DEFAULT_HOST);
        pixiv = new Pixiv(retrofit);

        pixiv.illustrationApi().getDetail(99147997).enqueue(new PixivCallback<>() {
            @Override
            public void onSuccess(IllustrationDetail body) {

            }
        });
    }


    private static String readText(File file) {
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            return reader.lines().collect(Collectors.joining());
        } catch (IOException e) {
            return null;
        }
    }
}
